package com.xiaoq.sitesticket.service.impl;

import com.xiaoq.sitesticket.entity.SitesTicketEntity;
import com.xiaoq.sitesticket.repository.ISitesTicketESRepository;
import com.xiaoq.sitesticket.repository.ISitesTicketRepository;
import com.xiaoq.sitesticket.service.ISitesTicketService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/10/24.
 */
@Service
public class SitesTicketServiceImpl implements ISitesTicketService{

    @Autowired
    private ISitesTicketRepository sitesTicketRepository;

    @Autowired
    private ISitesTicketESRepository sitesTicketESRepository;

    @Override
    public SitesTicketEntity findSitesTicketById(String id) {
        return sitesTicketRepository.findSitesTicketById(id);
    }

    @Override
    public List<SitesTicketEntity> findSitesTicketListBySitesId(String sitesId) {
        return sitesTicketRepository.findSitesTicketListBySitesId(sitesId);
    }

    @Override
    public List<SitesTicketEntity> findSitesTicketFromEsBySitesName(String sitesName) {
        PageRequest pageRequest = new PageRequest(0, 20);

        BoolQueryBuilder queryBuilder =  QueryBuilders.boolQuery()
                .must(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("sitesName", sitesName)
                        .fuzziness(Fuzziness.AUTO).prefixLength(1).maxExpansions(10).boost(0.98f))
                        .should(QueryBuilders.wildcardQuery("sitesShortName", "*"+sitesName+"*").boost(0.8f)));

        // 创建搜索 DSL 查询
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageRequest)
                .withQuery(queryBuilder).build();
        System.err.println(searchQuery.getQuery().toString());
        Page<SitesTicketEntity> searchPageResults = sitesTicketESRepository.search(searchQuery);
        return searchPageResults.getContent();
    }

    @Override
    public List<SitesTicketEntity> findTicketFromEsBySitesNameAndType(String sitesName, String consumerType) {
        if(StringUtils.isBlank(sitesName) || StringUtils.isBlank(consumerType)){
            return new ArrayList<>(0);
        }
        PageRequest pageRequest = new PageRequest(0, 10);
        BoolQueryBuilder queryBuilder =  QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("consumerType", consumerType))
                .must(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("sitesName", sitesName)
                        .fuzziness(Fuzziness.AUTO).prefixLength(1).maxExpansions(10).boost(0.98f))
                        .should(QueryBuilders.wildcardQuery("sitesShortName", "*"+sitesName+"*").boost(0.8f)));

        // 创建搜索 DSL 查询
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageRequest)
                .withQuery(queryBuilder).build();
        System.err.println(searchQuery.getQuery().toString());
        Page<SitesTicketEntity> searchPageResults = sitesTicketESRepository.search(searchQuery);
        return searchPageResults.getContent();
    }

}
